import hashlib
import json
import re
import uuid
from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit,
                             QInputDialog, QApplication)
import sys

from main import WechatApp


class Example(QWidget):

    def __init__(self):
        super().__init__()

    def verification(self, pwd):
        uid = str(uuid.uuid1()).split('-')[-1]
        pattern = re.compile('\d+')
        uid = re.search(pattern, uid).group()
        key = int(uid) * 38 + 85 - 23

        result = hashlib.md5(str(key).encode()).hexdigest()
        if pwd == result:
            return True
        else:
            return False

    def showDialog(self):
        uid = str(uuid.uuid1()).split('-')[-1]
        pattern = re.compile('\d+')
        uid = re.search(pattern, uid).group()
        text, ok = QInputDialog.getText(self, '软件激活',
                                        f'您的激活码为：{uid}\n获取激活码请加微信：init3323\n在下方输入您的激活码：:')

        if ok:
            result = self.verification(text)
            if result:
                return True
            else:
                return False

    @staticmethod
    def run():
        app = QApplication(sys.argv)
        ex = Example()
        result = ex.showDialog()
        if result:
            ec = WechatApp()
        else:
            Example.main()
        sys.exit(app.exec_())

if __name__ == '__main__':
    Example.run()